package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Rule;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("RuleDAO")
@Transactional
public class RuleDAOImpl extends AbstractJpaDao<Rule> implements RuleDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Rule.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public RuleDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Rule findRuleById(Integer id) throws DataAccessException {

		return findRuleById(id, -1, -1);
	}

	@Transactional
	public Rule findRuleById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRuleById", startResult, maxRows, id);
			return (Rule) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rule> findRuleByName(String name) throws DataAccessException {

		return findRuleByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findRuleByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRuleByName", startResult, maxRows, name);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@Transactional
	public Set<Rule> findRuleByNameContaining(String name) throws DataAccessException {

		return findRuleByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findRuleByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRuleByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@Transactional
	public Set<Rule> findRuleByDescription(String description) throws DataAccessException {

		return findRuleByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findRuleByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRuleByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@Transactional
	public Set<Rule> findRuleByDescriptionContaining(String description) throws DataAccessException {

		return findRuleByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findRuleByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRuleByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@Transactional
	public Rule findRuleByPrimaryKey(Integer id) throws DataAccessException {
		return findRuleByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Rule findRuleByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRuleByPrimaryKey", startResult, maxRows, id);
			return (Rule) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rule> findRulesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rule> findAllRules() throws DataAccessException {

		return findAllRules(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findAllRules(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllRules", startResult, maxRows);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Rule> findRulesBySite(Integer siteId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllRulesBySite", start, max, siteId);
		return new LinkedHashSet<Rule>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rule> findRuleByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Rule>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Rule> findRulesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Rule.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Rule> rules = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return rules;
	}

	@Transactional
	public Rule findRuleByPhysicalPrimaryKey(Integer siteId, String name) throws DataAccessException {
		return findRuleByPhysicalPrimaryKey(siteId, name, -1, -1);
	}

	@Transactional
	public Rule findRuleByPhysicalPrimaryKey(Integer siteId, String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findRuleByPhyscialPrimaryKey", startResult, maxRows, siteId, name);
			return (Rule) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Rule entity) {
		return true;
	}
}
